﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
using RealState.Utils;
using System.IO;


namespace RealState.WebUI.AdminPanel.plugins
{
    public partial class UploadImage : System.Web.UI.Page, IRequiresSessionState
    {
		//protected ILog log = LogManager.GetLogger(typeof(UploadImage));

        protected void btnUploadThumbnail_Click(object sender, EventArgs e)
        {
			try {
                int Width = 400;
                string s = "upload/items";
                string action = String.Empty;
				if (!string.IsNullOrEmpty(Request.QueryString["desiredWidth"]))
					Width = StringMethods.ToInt32(Request.QueryString["desiredWidth"].Trim());
                if (!string.IsNullOrEmpty(Request.QueryString["t"]))
                {
                    int type = StringMethods.ToInt32(Request.QueryString["t"].Trim());
                    switch (type)
                    {
                        case 1:
                            s = "upload/Logo";
                            break;
                        case 2:
                            s = "upload/Banners";
                            break;
                        case 3:
                            s = "upload/Ad";
                            break;
                        case 4:
                            s = "upload/Partner";
                            break;
                    }

                }
				
                if (Request.QueryString["sessionValue"] == "thumb")
                {
                    action = "parent.bindImagesThum();";
                    //Width = 400;
                }
                else if (Request.QueryString["sessionValue"] == "banners")
                {
                    action = "parent.bindImagesThum();";
                    s = "images/banners";
                    Width = 700;
                }
                else if (Request.QueryString["sessionValue"] == "gallery")
                {
                    s = "images/extensions";
                    Width = 300;
                }

                else if (Request.QueryString["sessionValue"] == "news")
                {
                    s = "upload/news";
                    Width = 305;
                }
				if (!fuThumbnail.HasFile)
					JavaScript.Alert("Vui lòng chọn hình ảnh để tải lên.", Page);
				else if (!Imaging.IsImageExtension(fuThumbnail.FileName))
				{
					JavaScript.Alert("Tập tin bạn muốn tải lên không phải là JPG, PNG hoặc GIF.", Page);
				}
				else
				{
					string fileName = fuThumbnail.FileName;
					byte[] buffer = Imaging.ResizeImageWithAspect(Width, fuThumbnail.FileBytes, fileName, Server.MapPath("~/temp/"), false);
					string str1 = StringMethods.Slash(s);
					string withoutExtension = Path.GetFileNameWithoutExtension(fileName);
					string str2 = withoutExtension + ".jpg";
					if (System.IO.File.Exists(Server.MapPath("~" + str1 + withoutExtension + ".jpg")))
					{
						int num = 1;
						while (true)
						{
							if (System.IO.File.Exists(Server.MapPath("~" + str1 + withoutExtension + "(" + num.ToString() + ").jpg")))
								++num;
							else
								break;
						}
						str2 = withoutExtension + "(" + num.ToString() + ").jpg";
					}
					using (FileStream fileStream = new FileStream(Server.MapPath("~" + str1 + str2), FileMode.CreateNew, FileAccess.Read | FileAccess.Write))
						fileStream.Write(buffer, 0, buffer.Length);
					Session["uploaded_pic"] = (object)str2;
					Session["uploaded_pic_ext"] = (object)StringMethods.NullToEmptyString(Request.QueryString["sessionValue"]);
                    if (action == String.Empty)
                    {
                        if (!Page.ClientScript.IsStartupScriptRegistered("ShowStatus1"))
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowStatus1", "parent.bindImages();", true);
                        }
                    }
                    else {
                        if (!Page.ClientScript.IsStartupScriptRegistered("ShowStatus"))
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowStatus", "parent.bindImagesThum();", true);
                        }
                    }
				}
			} catch (Exception ex) {
				//log.Error (ex.Message);
			}
            
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}
